package com.fenghongzhang.day006;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class Main2Activity extends AppCompatActivity {
    private ViewPager vp2;
    int index = 0;
    private  List<Fragment> list;
    private LinearLayout ll;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            //设置当前要显示fragment.参数下标
            vp2.setCurrentItem(index); //更新ui
            index++;
            if(index >= list.size()){
                index = 0;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        vp2 = (ViewPager) findViewById(R.id.vp2);

        ll = (LinearLayout) findViewById(R.id.ll);



        list  = new ArrayList<>();
        list.add(new OneFragment());
        list.add(new TwoFragment());
        list.add(new ThreeFragment());



        for (int i = 0; i < list.size(); i++) {
            if (i == 0){
                ImageView imageView = new ImageView(this);
                imageView.setImageResource(R.drawable.circle_t);
                ll.addView(imageView);
            }else{
                ImageView imageView = new ImageView(this);
                imageView.setImageResource(R.drawable.circle_f);
                ll.addView(imageView);
            }
        }



        int i = new Random().nextInt(11);

        MyFragmentAdapter myFragmentAdapter = new MyFragmentAdapter(getSupportFragmentManager(), list);
        vp2.setAdapter(myFragmentAdapter);

//        //设置当前要显示fragment.参数下标
//        vp2.setCurrentItem(2);

        //自动翻页
        //子线程
        final Timer timer = new Timer();
        timer.schedule(new TimerTask() {

            @Override
            public void run() {
               handler.sendEmptyMessage(0);
            }
        },0,1000);



        //翻页监听
        vp2.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //当前显示的页面
                //如果是最后一个页面,停止定时器
                if(position == list.size()-1){
                    timer.cancel();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        //翻页动画
        vp2.setPageTransformer(false,new DepthPageTransformer());

    }


}
